/*
 * NewACLInterface.java
 *
 * Created on May 12, 2006, 8:45 AM
 *
 * @author  Francois Daniels
 */

import java.awt.*;
import za.gateway.preferences.Settings;
import za.gateway.io.*;
import za.gateway.backend.*;
import za.gateway.container.*;

public class NewACLInterface extends javax.swing.JDialog {
    
    ConfigurationInterface ThisParentFrame = null;
    
    /** Creates new form NewACLInterface */
    public NewACLInterface (ConfigurationInterface parent, 
            boolean modal) {
        
        super (parent, modal);
        initComponents ();
        updateComponents ();
        ThisParentFrame = parent;
    }
    
    private void updateComponents () {
        readData ();
        centerOnParentAndShow ();
        if (Settings.isWindows) {
            try {
                javax.swing.UIManager.setLookAndFeel (
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                javax.swing.SwingUtilities.
                        updateComponentTreeUI (this);
                pack ();
            } catch (Exception e) { }
        }
    }
    
    public void setVisible (boolean b) {
        centerOnParentAndShow ();
        super.setVisible (b);
    }
    
    private void centerOnParentAndShow () {
        pack ();
        Container parent = getParent ();
        if (parent == null) return;
        
        Dimension dparent = parent.getSize ();
        Dimension window = getSize ();
        int xCoord, yCoord;
        try {
            xCoord = parent.getLocationOnScreen ().x + 
                    (dparent.width/2 - window.width/2);
            yCoord = parent.getLocationOnScreen ().y + 
                    (dparent.height/2 - window.height/2);
        } catch (IllegalComponentStateException e) {return;}
        
        //Ensure that no part of the window will be off-screen
        Dimension screen = 
                Toolkit.getDefaultToolkit ().getScreenSize ();
        int xOffScreenExcess = xCoord + window.width - screen.width;
        if ( xOffScreenExcess > 0 ) {
            xCoord = xCoord - xOffScreenExcess;
        }
        if (xCoord < 0 ) {
            xCoord = 0;
        }
        int yOffScreenExcess = yCoord + window.height - screen.height;
        if ( yOffScreenExcess > 0 ) {
            yCoord = yCoord - yOffScreenExcess;
        }
        if (yCoord < 0) {
            yCoord = 0;
        }
        
        setLocation ( xCoord, yCoord );
    }
    
    private void readData () {
        Restrictions r = new Restrictions (Settings.userName, 
                Settings.password, Settings.IP, Settings.database);
        Groups g = new Groups (Settings.userName, Settings.password, 
                Settings.IP, Settings.database);
        Phones p = new Phones (Settings.userName, Settings.password, 
                Settings.IP, Settings.database);
        SoftPhones s = new SoftPhones (Settings.userName, 
                Settings.password, Settings.IP, Settings.database);
        AccessControlList ACL = new AccessControlList (
                Settings.userName, Settings.password, Settings.IP, 
                Settings.database);
        
        Group [] groups = g.GetData ();
        Phone [] phones = p.GetData ();
        Restriction [] restrictions = r.GetData ();
        SoftPhone [] softPhones = s.GetData ();
        
        for (int i = 0; i < groups.length; i++)
            groupsList.add (groups [i].GetName ());
        
        for (int i = 0; i < phones.length; i++)
            phonesList.add (phones [i].GetName ());
        
        for (int i = 0; i < restrictions.length; i++)
            restrictionsList.add (restrictions [i].GetName ());
        
        for (int i = 0; i < softPhones.length; i++)
            phonesList.add (softPhones [i].GetNameData ());
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Code generated by Netbeans for the GUI Components  ">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        filterGroup = new javax.swing.ButtonGroup();
        listPanel = new javax.swing.JPanel();
        groupPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        buttonPanelG = new javax.swing.JPanel();
        addGroup = new javax.swing.JButton();
        groupsList = new java.awt.List();
        restrictionPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        buttonPanelR = new javax.swing.JPanel();
        addRestriction = new javax.swing.JButton();
        restrictionsList = new java.awt.List();
        phonePanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        buttonPanelP = new javax.swing.JPanel();
        addPhone = new javax.swing.JButton();
        phonesList = new java.awt.List();
        bottomPanel = new javax.swing.JPanel();
        filterPanel = new javax.swing.JPanel();
        filterBorder = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        nameRadio = new javax.swing.JRadioButton();
        locationRadio = new javax.swing.JRadioButton();
        phoneRadio = new javax.swing.JRadioButton();
        buttonPanel = new javax.swing.JPanel();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Access Control");
        listPanel.setLayout(new java.awt.GridLayout(1, 3, 8, 0));

        groupPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Groups");
        groupPanel.add(jLabel1, java.awt.BorderLayout.NORTH);

        addGroup.setText("New");
        addGroup.setMaximumSize(new java.awt.Dimension(65, 23));
        addGroup.setMinimumSize(new java.awt.Dimension(65, 23));
        addGroup.setPreferredSize(new java.awt.Dimension(65, 23));
        addGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addGroupActionPerformed(evt);
            }
        });

        buttonPanelG.add(addGroup);

        groupPanel.add(buttonPanelG, java.awt.BorderLayout.SOUTH);

        groupPanel.add(groupsList, java.awt.BorderLayout.CENTER);

        listPanel.add(groupPanel);

        restrictionPanel.setLayout(new java.awt.BorderLayout());

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Restrictions");
        restrictionPanel.add(jLabel2, java.awt.BorderLayout.NORTH);

        addRestriction.setText("New");
        addRestriction.setMaximumSize(new java.awt.Dimension(65, 23));
        addRestriction.setMinimumSize(new java.awt.Dimension(65, 23));
        addRestriction.setPreferredSize(new java.awt.Dimension(65, 23));
        addRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRestrictionActionPerformed(evt);
            }
        });

        buttonPanelR.add(addRestriction);

        restrictionPanel.add(buttonPanelR, java.awt.BorderLayout.SOUTH);

        restrictionPanel.add(restrictionsList, java.awt.BorderLayout.CENTER);

        listPanel.add(restrictionPanel);

        phonePanel.setLayout(new java.awt.BorderLayout());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Phones Numbers");
        phonePanel.add(jLabel3, java.awt.BorderLayout.NORTH);

        addPhone.setText("New");
        addPhone.setMaximumSize(new java.awt.Dimension(65, 23));
        addPhone.setMinimumSize(new java.awt.Dimension(65, 23));
        addPhone.setPreferredSize(new java.awt.Dimension(65, 23));
        addPhone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPhoneActionPerformed(evt);
            }
        });

        buttonPanelP.add(addPhone);

        phonePanel.add(buttonPanelP, java.awt.BorderLayout.SOUTH);

        phonePanel.add(phonesList, java.awt.BorderLayout.CENTER);

        listPanel.add(phonePanel);

        getContentPane().add(listPanel, java.awt.BorderLayout.CENTER);

        bottomPanel.setLayout(new java.awt.BorderLayout());

        filterPanel.setLayout(new java.awt.BorderLayout());

        filterBorder.setLayout(new java.awt.BorderLayout());

        filterBorder.setBorder(javax.swing.BorderFactory.createTitledBorder("Phone Filter"));
        jLabel4.setText("Filter: ");

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .add(15, 15, 15)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
        );
        filterBorder.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridLayout(3, 0, 0, 4));

        filterGroup.add(nameRadio);
        nameRadio.setSelected(true);
        nameRadio.setText("Name");
        nameRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        nameRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel2.add(nameRadio);

        filterGroup.add(locationRadio);
        locationRadio.setText("Location");
        locationRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        locationRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel2.add(locationRadio);

        filterGroup.add(phoneRadio);
        phoneRadio.setText("Phone Number");
        phoneRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        phoneRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel2.add(phoneRadio);

        filterBorder.add(jPanel2, java.awt.BorderLayout.EAST);

        filterPanel.add(filterBorder, java.awt.BorderLayout.CENTER);

        bottomPanel.add(filterPanel, java.awt.BorderLayout.CENTER);

        addButton.setText("Add");
        addButton.setMaximumSize(new java.awt.Dimension(65, 23));
        addButton.setMinimumSize(new java.awt.Dimension(65, 23));
        addButton.setPreferredSize(new java.awt.Dimension(65, 23));
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout buttonPanelLayout = new org.jdesktop.layout.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 281, Short.MAX_VALUE)
                .add(cancelButton)
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(buttonPanelLayout.createSequentialGroup()
                .add(buttonPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        bottomPanel.add(buttonPanel, java.awt.BorderLayout.SOUTH);

        getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose ();
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    private void addPhoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPhoneActionPerformed
        new NewPhoneInterface (this, true).setVisible (true);
    }//GEN-LAST:event_addPhoneActionPerformed
    
    private void addRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRestrictionActionPerformed
        new NewRestrictionInterface (this, true).setVisible (true);
    }//GEN-LAST:event_addRestrictionActionPerformed
    
    private void addGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addGroupActionPerformed
        new NewGroupInterface (this, true).setVisible (true);
    }//GEN-LAST:event_addGroupActionPerformed
    
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        Phones p = new Phones (Settings.userName, Settings.password, 
                Settings.IP, Settings.database);
        SoftPhones s = new SoftPhones (Settings.userName, 
                Settings.password, Settings.IP, Settings.database);
        AccessControlList ACL = new AccessControlList (
                Settings.userName, Settings.password, Settings.IP, 
                Settings.database);
        
        String groupName = groupsList.getSelectedItem ();
        String restrictionName = restrictionsList.getSelectedItem ();
        String phonesName = phonesList.getSelectedItem ();
        String phoneNumber = p.GetPhoneNumber (phonesName);
        if (phoneNumber == null)
            phoneNumber = s.GetPhoneNumber (phonesName);
        
        ACL.Add (groupName, restrictionName, phoneNumber);
        ACL.Save ();
        
        ThisParentFrame.update (true);
        dispose ();
    }//GEN-LAST:event_addButtonActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton addGroup;
    private javax.swing.JButton addPhone;
    private javax.swing.JButton addRestriction;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel buttonPanelG;
    private javax.swing.JPanel buttonPanelP;
    private javax.swing.JPanel buttonPanelR;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel filterBorder;
    private javax.swing.ButtonGroup filterGroup;
    private javax.swing.JPanel filterPanel;
    private javax.swing.JPanel groupPanel;
    private java.awt.List groupsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel listPanel;
    private javax.swing.JRadioButton locationRadio;
    private javax.swing.JRadioButton nameRadio;
    private javax.swing.JPanel phonePanel;
    private javax.swing.JRadioButton phoneRadio;
    private java.awt.List phonesList;
    private javax.swing.JPanel restrictionPanel;
    private java.awt.List restrictionsList;
    // End of variables declaration//GEN-END:variables
    
}
